/**
 * Description: SystemAnnouncementDAOImpl.java
 * Copyright (c) 2008-2009  Neo. 
 * All Rights Reserved.
 * @version 1.0  Mar 29, 2009 12:15:25 PM 李萌（neolimeng@gmail.com）created
 */
package org.paradise.dms.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.paradise.dms.pojo.SystemAnnouncement;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Service;

import com.dheaven.framework.dao.BaseHibernateDao;
import com.dheaven.framework.dao.DaoException;

/**
 * Description: Copyright (c) 2008-2009 Neo. All Rights Reserved.
 * 
 * @version 1.0 Mar 29, 2009 12:15:25 PM 李萌（neolimeng@gmail.com）created
 */
@Service
public class SystemAnnouncementDAOImpl extends BaseHibernateDao<SystemAnnouncement> {
	@SuppressWarnings("unused")
	private static Logger log = Logger.getLogger(StudentDAOImpl.class);

	/**
	 * 
	 * Description: DAO-增加一条公告
	 * 
	 * @Version1.0 Mar 29, 2009 12:26:36 PM 李萌（neolimeng@gmail.com）创建
	 * @param systemannouncement
	 * @throws DaoException
	 */
	public void insertSystemAnnouncement(SystemAnnouncement systemannouncement) throws DaoException {
		this.create(systemannouncement);
	}

	/**
	 * 
	 * Description: DAO-更新一条公告
	 * 
	 * @Version1.0 Mar 29, 2009 12:27:05 PM 李萌（neolimeng@gmail.com）创建
	 * @param systemannouncement
	 * @throws DaoException
	 */
	public void updateSystemAnnouncement(SystemAnnouncement systemannouncement) throws DaoException {
		this.update(systemannouncement);
	}

	/**
	 * 
	 * Description: DAO-删除一条公告
	 * 
	 * @Version1.0 Mar 29, 2009 12:27:23 PM 李萌（neolimeng@gmail.com）创建
	 * @param systemannouncementid
	 * @throws DaoException
	 */
	public void deleteSystemAnnouncement(int systemannouncementid) throws DaoException {
		SQLQuery sqlQuery = (SQLQuery) this.getSession().createSQLQuery("update systemannouncement set systemannouncementind=0 where systemannouncementid=?").setInteger(0, systemannouncementid);
		sqlQuery.executeUpdate();
	}

	/**
	 * 
	 * Description: DAO-根据公告ID取得一条公告
	 * 
	 * @Version1.0 Mar 29, 2009 12:27:46 PM 李萌（neolimeng@gmail.com）创建
	 * @param systemannouncementid
	 * @return
	 * @throws DaoException
	 */
	public SystemAnnouncement getSystemAnnouncement(int systemannouncementid) throws DaoException {
		return (SystemAnnouncement) this.find("from SystemAnnouncement where systemannouncementid=? and systemannouncementind=1", new Object[] { systemannouncementid }).get(0);
	}

	/**
	 * 
	 * Description: DAO-返回全部公告信息
	 * 
	 * @Version1.0 Mar 29, 2009 10:11:50 PM 李萌（neolimeng@gmail.com）创建
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<SystemAnnouncement> getAllSystemAnnouncement() {
		return this.find("from SystemAnnouncement where systemannouncementind = 1");
	}

	public int getAllRows() {
		return this.find("from SystemAnnouncement where systemannouncementind = 1").size();
	}

	@SuppressWarnings("unchecked")
	public List<SystemAnnouncement> listAllSystemAnnouncement(int pageSize, int startRow) {
		final int pageSize1 = pageSize;
		final int startRow1 = startRow;
		return this.getHibernateTemplate().executeFind(new HibernateCallback() {
			public List doInHibernate(Session session) throws HibernateException, SQLException {
				Query query = session.createQuery("from SystemAnnouncement where systemannouncementind = 1");
				query.setFirstResult(startRow1);
				query.setMaxResults(pageSize1);
				return query.list();
			}
		});
	}
}
